<?php

class ProprieteDAO extends DAO{


	function getById($id){
		try
		{
			$id = Validator::escapeSql($id);
			$query="select * from PROPRIETE where id=$id;";
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$propInfoDAO = $this->factory->getProprieteInfoDAO();
			$typePropDAO = $this->factory->getTypeProprieteDAO();
			$ListDAO = $this->factory->getListeDAO();
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = new Propriete((int)$Bean->ID);
				$thebean->setDefault($Bean->IS_DEFAULT);
				$thebean->setTypePropriete($typePropDAO->getById((int)$Bean->TYPE_PROPRIETE_ID));
				$thebean->setTypeEntiteId((int)$Bean->TYPE_ENTITE_ID);
				$thebean->setListe($ListDAO->getById((int)$Bean->LIST_ID));
				$thebean->setInfos($propInfoDAO->getAllByPropriete($thebean));
			}
		}
		catch(Exception $e)     {
			throw $e;
		}

		return $thebean;
	}
	function remove($Bean){
		try{
			$propInfoDAO = $this->factory->getProprieteInfoDAO();
			foreach ($Bean->getInfos() as $Info){
				$propInfoDAO->remove($Info);
			}
			$query="delete from PROPRIETE where id='".Validator::escapeSql($Bean->getId())."';";
			$this->pdo->exec($query);
			$bool=true;
		}catch(Exception $e)    {
			$bool=false;
		}
		return $bool;

	}
	function update($Bean){
		try{
			$propInfoDAO = $this->factory->getProprieteInfoDAO();
			foreach ($Bean->getInfos() as $Info){
				$propInfoDAO->update($Info);
			}

			$query = "update PROPRIETE set is_default=".Validator::escapeSql($Bean->isDefault()).", type_propriete_id=".Validator::escapeSql($Bean->getTypePropriete()->getId()).",Type_entite_id=".Validator::escapeSql($Bean->getTypeEntiteId()).",list_id=".Validator::escapeSql($Bean->getListe()->getId())." where id=".Validator::escapeSql($Bean->getId()).";";
			$this->pdo->exec($query);
			$bool=true;
		}
		catch(Exception $e)     {
			$bool=false;

		}
		return $bool;
	}
	function add($Bean){
		 
		try{
			 
			if (is_null($Bean->getId())){
				$propInfoDAO = $this->factory->getProprieteInfoDAO();
				foreach ($Bean->getInfos() as $Info){
					$propInfoDAO->add($Info);
				}
				$thebean = null;
				$insertQuery = "insert into PROPRIETE (is_default,type_propriete_id,Type_entite_id,list_id) values (".Validator::escapeSql($Bean->isDefault()).",".Validator::escapeSql($Bean->getTypePropriete()->getId()).",".Validator::escapeSql($Bean->getTypeEntiteId()).",".Validator::escapeSql($Bean->getListe()->getId()).");";
				$this->pdo->exec($insertQuery);
				$thebean = $this->getById((int)$this->pdo->lastInsertId());
			}
		}catch(Exception $e)    {$this->pdo->message;}
		return $thebean;
	}

	function All(){
		try
		{

			$BeanArray = array();
			$query="select * from PROPRIETE;";
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = $this->getById((int)$Bean->ID);
				array_push($BeanArray,$thebean);
			}
				
		}
		catch(Exception $e)     {
			throw $e;
		}

		return $BeanArray;

	}
	function getAllByTypeEntite($TypeEntiteBean){
		try
		{

			$BeanArray = array();
			$query="select * from PROPRIETE where type_entite_id=".Validator::escapeSql($TypeEntiteBean->getId()).";";
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = $this->getById((int)$Bean->ID);
				array_push($BeanArray,$thebean);
			}
				
		}
		catch(Exception $e)     {
			throw $e;
		}

		return $BeanArray;

	}
	function getAllByEntite($Entite){
		try
		{

			$BeanArray = array();
			$query="select * from PROPRIETE where type_entite_id=".Validator::escapeSql($Entite->getTypeEntite()->getId())." ;";
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$propValueDAO = $this->factory->getProprieteValueDAO();
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = $this->getById((int)$Bean->ID);
				$thebean->setValues($propValueDAO->getAllByProprieteByEntite($Entite,$thebean));
				array_push($BeanArray,$thebean);
			}
				
		}
		catch(Exception $e)     {
			throw $e;
		}

		return $BeanArray;
	}
}
?>